using System.Collections.Generic;
using System.Web.Mvc;
using MvcCms.Data;
using MvcCms.Service;
using MvcCms.Service.Cache;
using Microsoft.Practices.ServiceLocation;
namespace MvcCms.Service.Helpers
{
    public static class NavHelper
    {
        public static IEnumerable<ContentCategory> GetNavData(this HtmlHelper helper)
        {
            ICmsService service = ServiceLocator.Current.GetInstance<ICmsService>();
           
            IEnumerable<ContentCategory> categories = service.ListTopNav();

            return categories;
        }

        public static IEnumerable<ContentPage> ListSubPages(this HtmlHelper helper)
        {
            object oRouteCatId;
            if (helper.ViewContext.RouteData.DataTokens.TryGetValue("contentcatid", out oRouteCatId))
            {
               
                var service = ServiceLocator.Current.GetInstance<ICmsService>();

                var cacheService = ServiceLocator.Current.GetInstance<ICacheService>();

                var currentPathName = (string)helper.ViewContext.RouteData.Values["path"];
                var currentCat = cacheService.GetCorrectCategory(int.Parse(oRouteCatId.ToString()), currentPathName);
                int currentcontentCatId = currentCat.CategoryID;
                IEnumerable<ContentPage> subpages = service.ListSubPages(currentcontentCatId);
                return subpages;
            }
            return null;
        }

        public static IEnumerable<ContentCategory> GetTopNav(this HtmlHelper helper)
        {
            ICmsService service = ServiceLocator.Current.GetInstance<ICmsService>();

            return service.ListCategoriesHierarchy();
        }

        public static string GetCurrentPortalName(this HtmlHelper helper)
        {
            ICacheService cacheService = ServiceLocator.Current.GetInstance<ICacheService>();
            Portal portal = cacheService.GetCurrentPortal();
            return portal.DisplayName;
        }
    }
}